
from PIL import Image 
import numpy as np
import math

def asin2angle(x, th):
    if x < 0:
        return math.pi - angle  
    else:
        return angle

def move_center2orign(l,h,center, th):
    a = np.array([0,0])
    b = np.array([l,0])
    c = np.array([l,h])
    d = np.array([0,h])

    a = a - center
    b = b - center
    c = c - center 
    d = d - center
    r_a = math.sqrt(a[0]**2+a[1]**2)
    r_b = math.sqrt(b[0]**2+b[1]**2)
    r_c = math.sqrt(c[0]**2+c[1]**2)
    r_d = math.sqrt(d[0]**2+d[1]**2)
    alpha_a = math.asin(a[0]/r_a)
    alpha_b = math.asin(b[0]/r_b)
    alpha_c = math.asin(c[0]/r_c)
    alpha_d = math.asin(d[0]/r_d)
    
    alpha_a = asin2angle(a[0], alpha_a)
    alpha_b = asin2angle(b[0], alpha_b)
    alpha_c = asin2angle(c[0], alpha_c)
    alpha_d = asin2angle(d[0], alpha_d)
    
    alpha_a_ = alpha_a + th
    alpha_b_ = alpha_b + th
    alpha_c_ = alpha_c + th
    alpha_d_ = alpha_d + th
    
    a_ = (r_a*math.cos(alpha_a_), r_a*math.sin(alpha_a_))
    b_ = (r_b*math.cos(alpha_b_), r_b*math.sin(alpha_b_))
    c_ = (r_c*math.cos(alpha_c_), r_c*math.sin(alpha_c_))
    d_ = (r_d*math.cos(alpha_d_), r_d*math.sin(alpha_d_))
    x = np.array([a_[0],b_[0], c_[0], d_[0]])
    y = np.array([a_[1],b_[1], c_[1], d_[1]])
    print(a_,b_,c_,d_)
    print("min:", x.min(),y.min())
    print("max:", x.max(),y.max())
    print("with:", x.max()-x.min())
    print("hight:", y.max()-y.max())
    print("=================================\n\n")
    
img = Image.open("needle.png")
index = list(range(45,60))+list(range(0,45))
for (i, angle) in enumerate(range(0,-360,-6)):
    out = img.rotate(angle, expand=True)
    out.save("needle_"+str(index[i])+".png")
    move_center2orign(img.size[0], img.size[1], np.array([166,13]), angle)